<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    李梓钿
 *
 * 系统会员相关类
 * 
 */
namespace osc\common\service;
use think\Db;
use think\Session;
use think\Loader;
class Stone{	

	


	function stone_wd($data)
	{

		$username = session("username");
		$uid = is_login();

		$validate = Loader::validate('UserInfo');


		if(!$username)
		{
			return array('error'=>'获取用户信息失败');
		}

		if (!$validate->scene('goldstone')->check($data)) {
            //throw new \Exception($validate->getError());
            return array('error'=>$validate->getError());
        }
        //var_dump($data);


        $pwd2 = think_ucenter_encrypt($data['pwd2'],config('PWD_KEY'));


         $user=Db::name('member')->where('username',$username)->find();

         if($pwd2!=$user['2password'])
         {
         	return array('error'=>'二级密码输入有误');
         }

         $bank = Db::name('user_bank')->where('username',$username)->find();
         if(!$bank)
         {
         	return array('error'=>'您还没有设置银行账号!');
         }
        //事务开始
		Db::startTrans();
         //var_dump($bank);
        $goldstone['username'] = $username;
        $goldstone['token'] = $data['token'];
        $goldstone['num'] = $data['num'];
        $goldstone['creat_time'] = time();
        $goldstone['money']= $data['num'];
        $goldstone['real_money'] = $goldstone['num']*0.95;
        $goldstone['description'] = '回购';
		$goldstone['bankname'] = $bank['bankname'].$bank['subbankname'];
		$goldstone['bankaccountname'] = $bank['bankaccountname'];
		$goldstone['bankaccount'] = $bank['bankaccount'];


        $goldstoneinfo['username']= $goldstone['username'];
        $goldstoneinfo['num']= $goldstone['num'];
        $goldstoneinfo['creat_time']= $goldstone['creat_time'];
        $goldstoneinfo['description']= '提交回购';
		
         if($user['goldstone']<$goldstone['num'])
         {
         	return array('error'=>'您的可用额度不够!');
         }



        try {
        	//var_dump($goldstone);
        	$user=Db::name('stonewd')->insert($goldstone);
        	//var_dump($user);
			$user=Db::name('stone_wd_rcd')->insert($goldstoneinfo);
        	$user=Db::name('stone_position')->where('username',$goldstone['username'])->setDec('position', $goldstone['num']);
        	$user=Db::name('stone_position')->where('username',$goldstone['username'])->setInc('use_position', $goldstone['num']);
        	storage_user_action($uid,$username,config('FRONTEND_USER'),'用户提现');
            Db::commit();
        } catch (\Exception $e) {

            Db::rollback();
            return array('error'=>$e->getMessage());

       }

	}


	function stone_goldstone($data)
	{
		$username = session("username");
		$uid = is_login();

		$validate = Loader::validate('UserInfo');
		if(!$username)
		{
			return array('error'=>'获取用户信息失败');
		}

		if (!$validate->scene('stone')->check($data)) {
            //throw new \Exception($validate->getError());
            return array('error'=>$validate->getError());
        }

        $pwd2 = think_ucenter_encrypt($data['pwd2'],config('PWD_KEY'));


         $user=Db::name('member')->where('username',$username)->find();

         if($pwd2!=$user['2password'])
         {
         	return array('error'=>'二级密码输入有误');
         }

        //事务开始
		 Db::startTrans();
         $user=Db::name('member')->where('username',$username)->find();
         $stone=Db::name('stone')->where('id',$data['id'])->find();

         if(!$stone)
         {
         	return array('error'=>'获取信息失败');
         }
         if($stone['state']==1)
         {
         	return array('error'=>'获取信息失败');
         }

         $pre_bala = $data['num']*$stone['curr_price'];
         if(($user['goldstone']*200)<($pre_bala*0.9))
         {
         	return array('error'=>'您的可以额度不够!');
         }

         $num = ceil(($pre_bala*0.9)/200);

         //$remainder = (($user['goldstone']*200 - ($pre_bala*0.9))/0.9;
         $remainder = $num*200-$pre_bala*0.9;
         $remainder = $remainder*1.1;


        try {

	         $stone_position = Db::name('stone_position')->where('username',$username)->find();
	         if($stone_position)
	         {
	         	//update
	            $ret=Db::name('stone_position')->where('username',$username)->setInc('position',  $remainder);
	         }
	         else
	         {
	         	//insert
	         	$sp['position'] = $remainder;
	         	$sp['username'] = $username;
	         	$sp['creat_time']= time();
	         	$sp['use_position'] = 0;
	         	$ret=Db::name('stone_position')->insert($sp);

	         }

	         $user_stone = Db::name('user_stone')->where('username',$username)->where('stone_id',$data['id'])->find();


	         if($user_stone)
	         {
	         	$ret = Db::name('user_stone')->where('username',$username)->where('stone_id',$data['id'])->setInc('num',  $data['num']);
	         }else
	         {
	         	$us['username'] = $username;
	         	$us['num'] = $data['num'];
	         	$us['stone_name'] = $stone['name'];
	         	$us['stone_id'] = $data['id'];
	         	$us['creat_time']= time();

				$ret=Db::name('user_stone')->insert($us);

	         }

	         //减掉金石
	         $ret=Db::name('member')->where('username',$username)->setDec('goldstone',$num);


	        $stoneinfo['username']= $username;
        	$stoneinfo['num']= $data['num'];
        	$stoneinfo['money']= $num;
        	$stoneinfo['last_money']= $remainder;
        	$stoneinfo['creat_time']= time();
        	$stoneinfo['token']= $data['token'];
        	$stoneinfo['description']= '巨能果增值买入价'.$stone['curr_price']; 

        	//var_dump($stoneinfo);
			$user=Db::name('stone_goldstone_tf')->insert($stoneinfo);

        	
        	storage_user_action($uid,$username,config('FRONTEND_USER'),'巨能果增值'.$stone['name']);
            Db::commit();
            //return array('sucess'=>'操作成功!');
        } catch (\Exception $e) {

            Db::rollback();
            return array('error'=>$e->getMessage());

        }
	}


	function stone_sell($data)
	{
		$username = session("username");
		$uid = is_login();

		$validate = Loader::validate('UserInfo');
		if(!$username)
		{
			return array('error'=>'获取用户信息失败');
		}

		if (!$validate->scene('stone')->check($data)) {
            //throw new \Exception($validate->getError());
            return array('error'=>$validate->getError());
        }

        $pwd2 = think_ucenter_encrypt($data['pwd2'],config('PWD_KEY'));


         $user=Db::name('member')->where('username',$username)->find();

         if($pwd2!=$user['2password'])
         {
         	return array('error'=>'二级密码输入有误');
         }

        //事务开始
		 Db::startTrans();
         $user=Db::name('member')->where('username',$username)->find();
         $stone=Db::name('stone')->where('id',$data['id'])->find();

         if(!$stone)
         {
         	return array('error'=>'获取信息失败');
         }

         if($stone['state']==1)
         {
         	return array('error'=>'获取信息失败');
         }

        $userstone=Db::name('user_stone')->where('stone_id',$data['id'])->find();

        if($userstone['num']<$data['num'])
        {
        	return array('error'=>'提交的数量超过了用户持有数量');
        }

        //
        $predeposit = $stone['curr_price'] * $data['num'];

        try {
        	//减掉用户的份额

        	$ret = Db::name('user_stone')->where('username',$username)->where('stone_id',$data['id'])->setDec('num',  $data['num']);


	         $stone_position = Db::name('stone_position')->where('username',$username)->find();
	         $fee=$predeposit*0.02;
	         $predeposit = $predeposit-$fee;
	         if($stone_position)
	         {
	         	//update
	            $ret=Db::name('stone_position')->where('username',$username)->setInc('position',  $predeposit);
	         }
	         else
	         {
	         	//insert
	         	$sp['position'] = $predeposit;
	         	$sp['username'] = $username;
	         	$sp['creat_time']= time();
	         	$sp['use_position'] = 0;
	         	$ret=Db::name('stone_position')->insert($sp);

	         }

	         //$fee=$predeposit*0.03;

            
	        $stoneinfo['username']= $username;
        	$stoneinfo['num']= $data['num'];
        	$stoneinfo['position']= $predeposit;
        	$stoneinfo['price']= $stone['curr_price'];
        	$stoneinfo['creat_time']= time();
        	$stoneinfo['token']= $data['token'];
        	$stoneinfo['description']= '出售价格'.$stone['curr_price'].',手续费'.$fee; 
			
        	//var_dump($stoneinfo);
			$user=Db::name('stone_position_tf')->insert($stoneinfo);

        	
        	storage_user_action($uid,$username,config('FRONTEND_USER'),'增值出售'.$stone['name']);
            Db::commit();
            //return array('sucess'=>'操作成功!');
        } catch (\Exception $e) {

            Db::rollback();
            return array('error'=>$e->getMessage());

        }
	}         

	

	  
	function percentEncode($str)  
	{  
		// 使用urlencode编码后，将"+","*","%7E"做替换即满足ECS API规定的编码规范  
		$res = urlencode($str);  
		$res = preg_replace('/\+/', '%20', $res);  
		$res = preg_replace('/\*/', '%2A', $res);  
		$res = preg_replace('/%7E/', '~', $res);  
		return $res;  
	}  

	
}
